package biz

import (
	"time"
	"transfer-service/internal/global"
	"transfer-service/internal/model/const/anti"

	mqtt "github.com/eclipse/paho.mqtt.golang"
	"github.com/go-kratos/kratos/v2/log"
)

type SpectrumUseCase struct {
	log *log.Helper
}

func NewSpectrumUseCase(logger log.Logger,
) *SpectrumUseCase {
	return &SpectrumUseCase{
		log: log.NewHelper(log.With(logger, "module", "internal/biz/Spectrum")),
	}
}

func (s *SpectrumUseCase) TransferData() {
	address := global.BC.GetMiddleware().GetMqtt().GetGovNet().GetAddress()
	val, ok := global.MQTTList.Load(address)
	if !ok {
		global.LOG.Error("从mqtt map中获取客户端异常: address: %s", address)
		return
	}
	client := val.(mqtt.Client)
	token := client.Subscribe(anti.SPECTRUM_SYS_INFO_TOPIC, 0, s.handlerSysInfo)
	if !token.WaitTimeout(5 * time.Second) {
		s.log.Errorf("订阅主题 %s 超时", anti.SPECTRUM_SYS_INFO_TOPIC)
		return
	}
	if token.Error() != nil {
		s.log.Errorf("订阅主题 %s 失败: %v", anti.SPECTRUM_SYS_INFO_TOPIC, token.Error())
	} else {
		s.log.Infof("成功订阅主题: %s,broker 地址：%s", anti.SPECTRUM_SYS_INFO_TOPIC, address)
	}

	token = client.Subscribe(anti.SPECTRUM_LOCATE_TOPIC, 0, s.handlerLocate)
	if !token.WaitTimeout(5 * time.Second) {
		s.log.Errorf("订阅主题 %s 超时", anti.SPECTRUM_LOCATE_TOPIC)
		return
	}
	if token.Error() != nil {
		s.log.Errorf("订阅主题 %s 失败: %v", anti.SPECTRUM_LOCATE_TOPIC, token.Error())
	} else {
		s.log.Infof("成功订阅主题: %s,broker 地址：%s", anti.SPECTRUM_LOCATE_TOPIC, address)
	}

	token = client.Subscribe(anti.SPECTRUM_DETECT_TOPIC, 0, s.handlerDetect)
	if !token.WaitTimeout(5 * time.Second) {
		s.log.Errorf("订阅主题 %s 超时", anti.SPECTRUM_DETECT_TOPIC)
		return
	}
	if token.Error() != nil {
		s.log.Errorf("订阅主题 %s 失败: %v", anti.SPECTRUM_DETECT_TOPIC, token.Error())
	} else {
		s.log.Infof("成功订阅主题: %s,broker 地址：%s", anti.SPECTRUM_DETECT_TOPIC, address)
	}
	return
}

func (s *SpectrumUseCase) handlerSysInfo(c mqtt.Client, message mqtt.Message) {
	s.log.Info("接收到频谱设备系统信息:", string(message.Payload()))
	address := global.BC.GetMiddleware().GetMqtt().GetVideoNet().GetAddress()
	val, ok := global.MQTTList.Load(address)
	if !ok {
		global.LOG.Error("从mqtt map中获取客户端异常: address: %s", address)
		return
	}
	client := val.(mqtt.Client)
	token := client.Publish(anti.SPECTRUM_SYS_INFO_TOPIC, 0, false, message.Payload())
	if !token.WaitTimeout(5 * time.Second) {
		s.log.Errorf("发布到主题 %s 超时", anti.SPECTRUM_SYS_INFO_TOPIC)
		return
	}
	if token.Error() != nil {
		s.log.Errorf("发布到主题 %s 失败: %v", anti.SPECTRUM_SYS_INFO_TOPIC, token.Error())
	} else {
		s.log.Infof("成功发布到主题: %s,broker 地址：%s", anti.SPECTRUM_SYS_INFO_TOPIC, address)
	}
}

func (s *SpectrumUseCase) handlerLocate(c mqtt.Client, message mqtt.Message) {
	s.log.Info("接收到频谱设备系统信息:", string(message.Payload()))
	address := global.BC.GetMiddleware().GetMqtt().GetVideoNet().GetAddress()
	val, ok := global.MQTTList.Load(address)
	if !ok {
		global.LOG.Error("从mqtt map中获取客户端异常: address: %s", address)
		return
	}
	client := val.(mqtt.Client)
	s.log.Info("接收到频谱设备定位信息:", string(message.Payload()))
	token := client.Publish(anti.SPECTRUM_LOCATE_TOPIC, 0, false, message.Payload())
	if !token.WaitTimeout(5 * time.Second) {
		s.log.Errorf("发布到主题 %s 超时", anti.SPECTRUM_LOCATE_TOPIC)
		return
	}
	if token.Error() != nil {
		s.log.Errorf("发布到主题 %s 失败: %v", anti.SPECTRUM_LOCATE_TOPIC, token.Error())
	} else {
		s.log.Infof("成功发布到主题: %s,broker 地址：%s", anti.SPECTRUM_LOCATE_TOPIC, address)
	}
}

func (s *SpectrumUseCase) handlerDetect(c mqtt.Client, message mqtt.Message) {
	s.log.Info("接收到频谱设备系统信息:", string(message.Payload()))
	address := global.BC.GetMiddleware().GetMqtt().GetVideoNet().GetAddress()
	val, ok := global.MQTTList.Load(address)
	if !ok {
		global.LOG.Error("从mqtt map中获取客户端异常: address: %s", address)
		return
	}
	client := val.(mqtt.Client)
	s.log.Info("接收到频谱设备侦测信息:", string(message.Payload()))
	token := client.Publish(anti.SPECTRUM_DETECT_TOPIC, 0, false, message.Payload())
	if !token.WaitTimeout(5 * time.Second) {
		s.log.Errorf("发布到主题 %s 超时", anti.SPECTRUM_DETECT_TOPIC)
		return
	}
	if token.Error() != nil {
		s.log.Errorf("发布到主题 %s 失败: %v", anti.SPECTRUM_DETECT_TOPIC, token.Error())
	} else {
		s.log.Infof("成功发布到主题: %s,broker 地址：%s", anti.SPECTRUM_DETECT_TOPIC, address)
	}
}
